home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
vbdatabs
/
terminal.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-03-14
|
16KB
|
716 lines
// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- //
// C++ Source Code File Name: terminal.cpp
// Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
// Produced By: Doug Gaer
// File Creation Date: 03/21/1997
// Date Last Modified: 03/15/1999
// Copyright (c) 1997 Douglas M. Gaer
// ----------------------------------------------------------- //
// ------------- Program Description and Details ------------- //
// ----------------------------------------------------------- //
/*
The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
All those who put this code or its derivatives in a commercial
product MUST mention this copyright in their documentation for
users of the products in which this code or its derivative
classes are used. Otherwise, you have the freedom to redistribute
verbatim copies of this source code, adapt it to your specific
needs, or improve the code and release your improvements to the
public provided that the modified files carry prominent notices
stating that you changed the files and the date of any change.
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
CORRECTION.
This is a terminal interface designed to be portable between
DOS and UNIX systems. On UNIX systems the "curses" library is
used to create terminal independent code. On MSDOS/Windows95
systems the ANSI.SYS driver is used to simulate the basic
functions of the "curses" library.
*/
// ----------------------------------------------------------- //
#include <ctype.h>
#include <string.h>
#include "terminal.h"
#ifdef __DOS__
#include <iostream.h>
#include <iomanip.h>
#endif
#ifdef __UNIX__
#include <sys/time.h>
#include <termio.h>
#endif
Terminal I_TERM; // Independent Teminal type object
Terminal *terminal = &I_TERM; // Global terminal pointer
void Terminal::GetString (char *string, int x, int y)
// This function allows the user to enter a single string.
// It was added to echo each character without having to
// to turn the echo on. This will allow the uesr to edit
// the entry using the backspace or other defined keys.
{
char ch;
int charcount = 0;
if (x != -1) move (y, x);
while (1) {
ch = getch();
if (ch == '\n' || ch == '\r' || ch == CONTROL('c')) {
*string = 0;
return;
}
if (ch == '\b' || ch == CONTROL('d')) { // Delete keys
if (charcount > 0) {
string--;
charcount--;
Write('\b');
Write(' ');
Write('\b');
}
}
else {
*string++ = ch;
charcount++;
Write(ch);
}
refresh();
}
}
void Terminal::GetPassword (char *string, int x, int y)
// This function allows the user to enter a password
// and echo an asterisk for each character typed.
{
char ch;
int charcount = 0;
if (x != -1) move (y, x);
while (1) {
ch = getch();
if (ch == '\n' || ch == '\r' || ch == CONTROL('c')) {
*string = 0;
return;
}
if (ch == '\b' || ch == CONTROL('d')) { // Delete keys
if (charcount > 0) {
string--;
charcount--;
Write('\b');
Write(' ');
Write('\b');
}
}
else {
*string++ = ch;
charcount++;
Write('*');
}
refresh();
}
}
int Terminal::GetInt(int x, int y)
// Get a signed int value.
{
char buf[100];
GetString(buf, x, y);
return atoi(buf);
}
long Terminal::GetLong(int x, int y)
// Get a signed long value.
{
char buf[100];
GetString(buf, x, y);
return atol(buf);
}
double Terminal::GetFloat(int x, int y)
// Get a floating point value
{
char buf[100];
GetString(buf, x, y);
return atof(buf);
}
int Terminal::YesNo(int x, int y)
{
if(x == -1)
Write(" <Do you wish to continue (y/n)> "); // Same line message
else
Write("Do you wish to continue (y/n)...", x, y);
char c = ' ';
while (c != 'y' && c != 'n') {
c = GetChar();
c = tolower(c);
}
return c == 'y' ? 1: 0; // return true if users answers yes
}
int Terminal::YesNo(const char *s, int x, int y)
{
if(x == -1)
Write(s); // Same line message
else
Write(s, x, y);
char c = ' ';
while (c != 'y' && c != 'n') {
c = GetChar();
c = tolower(c);
}
return c == 'y' ? 1: 0; // return true if users answers yes
}
int Terminal::GetYesNo()
// Wait for y/n reply with no prompt.
{
char c = ' ';
while (c != 'y' && c != 'n') {
c = GetChar();
c = tolower(c);
}
return c == 'y' ? 1: 0; // return true if users answers yes
}
void Terminal::PutBack(char c)
// Put back a keyboard character
{
putback = c;
}
int Terminal::Center(const char *s) const
// Returns the center coordinate for a string.
{
int len = strlen(s);
if(len > MaxCols()-1) len = MaxCols()-1;
int mid = MaxCols()/2 - 1;
int half = len/2;
return mid - half;
}
int Terminal::Center(char *s)
// Returns the center coordinate for a string.
{
int len = strlen(s);
if(len > MaxCols()-1) len = MaxCols()-1;
int mid = MaxCols()/2 - 1;
int half = len/2;
return mid - half;
}
int Terminal::ScreenCenter(const int offset) const
// Returns the center coordinate for the screen.
{
int buf = offset;
if(buf > MaxLines()-1) buf = MaxLines()-1;
int mid = MaxLines()/2 - 1;
int half = buf/2;
return mid - half;
}
int Terminal::ScreenCenter(int offset)
// Returns the center coordinate for the screen.
{
if(offset > MaxLines()-1) offset = MaxLines()-1;
int mid = MaxLines()/2 - 1;
int half = offset/2;
return mid - half;
}
int Terminal::Right(const char *s) const
// Returns the right justifed coordinate for a string.
{
int len = strlen(s);
if(len > MaxCols()-1) len = MaxCols()-1;
int end = MaxCols()-1;
return end - len;
}
int Terminal::Right(char *s)
// Returns the right justifed coordinate for a string.
{
int len = strlen(s);
if(len > MaxCols()-1) len = MaxCols()-1;
int end = MaxCols()-1;
return end - len;
}
void Terminal::StatusLine(const char *s) const
// Display a status line in normal text mode.
{
Write(s, 0, MaxLines()-1);
int len = strlen(s);
while (len++ < MaxCols()-1) Write(' ');
}
void Terminal::StatusLine(char *s)
// Display a status line in normal text mode.
{
Write(s, 0, MaxLines()-1);
int len = strlen(s);
while (len++ < MaxCols()-1) Write(' ');
}
void Terminal::ClearLine(int x, int y) const
{
int i = 0;
if(x == -1)
while (i++ < MaxCols()-1) Write(' ');
else {
MoveCursor(x, y);
while (i++ < MaxCols()-1) Write(' ');
}
}
void Terminal::ClearLine(int x, int y)
{
int i = 0;
if(x == -1)
while (i++ < MaxCols()-1) Write(' ');
else {
MoveCursor(x, y);
while (i++ < MaxCols()-1) Write(' ');
}
}
void Terminal::MoveCursor(int x, int y) const
{
move(y, x);
refresh();
}
void Terminal::MoveCursor(int x, int y)
{
move(y, x);
refresh();
}
void Terminal::ClearScreen() const
{
clear();
}
void Terminal::ClearScreen()
{
clear();
}
void Terminal::AnyKey(int x, int y)
// Prompt the user for any key to continue
{
if(x == -1) {
Write(" <Press any key to continue> "); // Same line message
GetChar();
return;
}
Write("Press any key to continue...", x, y);
GetChar();
}
void Terminal::AnyKey(const char *s, int x, int y)
// Prompt the user for any key to continue
{
if(x == -1) {
Write(s); // Same line message
GetChar();
return;
}
Write(s, x, y);
GetChar();
}
void Terminal::Write(const unsigned char c, int x, int y) const
// Write a unsigned character to the screen.
{
if (x != -1) move (y, x);
char s[1];
s[0] = c; // Copy char into a string buffer
s[1] = '\0'; // Null term